package com.xkdx.guangguang.fragment.brand.search;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.BrandActivity;
import com.xkdx.guangguang.fragment.brand.BrandInfoFragment;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.BrandDetail;

public class SearchBrandResultFragment extends Fragment {

	View view;
	ListView listView;
	List<BrandDetail> list;
	Button back;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	SearchResultAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		list = ((BrandActivity) getActivity()).getSearchBrandList();
		options = new DisplayImageOptions.Builder().showStubImage(R.drawable.focuse_brand_discount)
				.showImageForEmptyUri(R.drawable.focuse_brand_discount)
				.showImageOnFail(R.drawable.focuse_brand_discount).resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.search_brand_result, container, false);
		IConstants.hideInputMethodAways(getActivity(), view);
		findView();
		setOnclick();
		return view;
	}

	private void findView() {
		back = (Button) view.findViewById(R.id.search_brand_back);
		listView = (ListView) view.findViewById(R.id.search_brand_result);
		adapter=new SearchResultAdapter(getActivity(), list);
		listView.setAdapter(adapter);
	}

	private void setOnclick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();

			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				BrandInfoFragment brandShopFragment = new BrandInfoFragment();
				Bundle b = new Bundle();
				b.putString("brandID", String.valueOf(list.get(position).getBrandID()));
				brandShopFragment.setArguments(b);
				ft.replace(R.id.fragment, brandShopFragment);
				ft.addToBackStack(null);
				ft.commit();
			}

		});

	}

	class SearchResultAdapter extends BaseAdapter {

		String url;
		Context context;
		List<BrandDetail> list;

		public SearchResultAdapter(Context context, List<BrandDetail> list) {
			super();
			this.list = list;
			this.context = context;
			// asyncImageLoader = new AsyncImageLoader(getActivity());
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final ViewHodler viewHolder;

			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.brand_search_item, null);
				viewHolder = new ViewHodler();
				viewHolder.image = (ImageView) convertView.findViewById(R.id.brand_search_item_im);
				viewHolder.name = (TextView) convertView.findViewById(R.id.brand_search_item_brandname);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHodler) convertView.getTag();
			}
			url = list.get(position).getLogo();
			// url = Static.addSuffix(url, "_logo");
			url = list.get(position).getLogo();
			// Log.i("BrandFragment", url);
			viewHolder.image.setTag(url);

			String brandChina = list.get(position).getBrandName();
			if (brandChina == null || brandChina.equals("null")) {
				brandChina = "";
			}
			String brandEn = list.get(position).getBrandNameEn();
			if (brandEn == null || brandEn.equals("null")) {
				brandEn = "";
			}
			String brandname = brandChina + brandEn;
			viewHolder.name.setText(brandname);

			imageLoader.displayImage(url, viewHolder.image, options);

			return convertView;

		}

		class ViewHodler {
			TextView name;
			ImageView image;
			LinearLayout ll;
		}
	}

}
